/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2011-1-14 下午04:34:38
 *
 * ChangeEmailAction.java
 *
 *******************************************************************/
package com.transer.web.action.passport;

import org.apache.log4j.Logger;

import com.transer.persistance.dao.IUserDao;
import com.transer.utils.MD5;
import com.transer.web.action.JsonActionSupport;

/**
 * class defination are there
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class ChangePasswordAction extends JsonActionSupport {

	private static final Logger logger = Logger.getLogger(ChangePasswordAction.class);

	private static final long serialVersionUID = 3518599158920775725L;
	// 用户操作
	private IUserDao userDao;
	// 旧密码
	private String oldpass;
	// 新密码
	private String newpass;
	// 确认密码
	private String confirmpass;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		try {
			// 旧密码
			oldpass = MD5.getMD5(oldpass);

			// 旧密码不正确
			if (!user.getPassword().equals(oldpass)) {
				// 返回消息
				writeSimpleJsonMessage(false, getText("error.oldpass"));
			} else if (!newpass.equals(confirmpass)) {// 两次输入的密码不相同
				// 返回消息
				writeSimpleJsonMessage(false, getText("error.confirmpass"));
			} else {
				// 设置新密码
				user.setPassword(MD5.getMD5(newpass));
				// 更新用户
				userDao.updateUser(user);
				// 返回消息
				writeSimpleJsonMessage(true, getText("success.passwd"));
			}
		} catch (Exception e) {
			// e.printStackTrace();
			// 记录日志
			logger.error("更改密码错误", e);
			// 返回消息
			writeSimpleJsonMessage(false, getText("error.confirmpass"));
		}
		return null;
	}

	/**
	 * @return the oldpass
	 */
	public String getOldpass() {
		return oldpass;
	}

	/**
	 * @param oldpass
	 *            the oldpass to set
	 */
	public void setOldpass(String oldpass) {
		this.oldpass = oldpass;
	}

	/**
	 * @return the newpass
	 */
	public String getNewpass() {
		return newpass;
	}

	/**
	 * @param newpass
	 *            the newpass to set
	 */
	public void setNewpass(String newpass) {
		this.newpass = newpass;
	}

	/**
	 * @return the confirmpass
	 */
	public String getConfirmpass() {
		return confirmpass;
	}

	/**
	 * @param confirmpass
	 *            the confirmpass to set
	 */
	public void setConfirmpass(String confirmpass) {
		this.confirmpass = confirmpass;
	}

	/**
	 * @return the userDao
	 */
	public IUserDao getUserDao() {
		return userDao;
	}

	/**
	 * @param userDao
	 *            the userDao to set
	 */
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
}
